﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SQLite;

namespace Gestion_Incidents_SI
{
    public partial class listeInterventions : Form
    {
        
        static string db = "Data Source=./si.db;Version=3";
        static SQLiteConnection m_dbConnection;
        
        public listeInterventions()
        {
            InitializeComponent();
        }

        private void listeInterventions_Load(object sender, EventArgs e)
        {
            m_dbConnection = new SQLiteConnection(db);
            m_dbConnection.Open();

            this.listView1.View = System.Windows.Forms.View.Details;
            

            ColumnHeader columnHeader0 = new ColumnHeader();
            columnHeader0.Text = "#";
            columnHeader0.Width = 20;

            ColumnHeader columnHeader1 = new ColumnHeader();
            columnHeader1.Text = "Date d'intervention";
            columnHeader1.Width = 180;

            ColumnHeader columnHeader2 = new ColumnHeader();
            columnHeader2.Text = "Service";
            columnHeader2.Width = 180;

            ColumnHeader columnHeader3 = new ColumnHeader();
            columnHeader3.Text = "Machine";
            columnHeader3.Width = 200;


            listView1.Columns.Add(columnHeader0);
            listView1.Columns.Add(columnHeader1);
            listView1.Columns.Add(columnHeader2);
            listView1.Columns.Add(columnHeader3);

            string query = string.Format(@"SELECT * FROM interventions, machines, services
                                            WHERE interventions.machine = machines.id_machine
                                            AND machines.service = services.id_service");
        
            SQLiteCommand command = new SQLiteCommand(query, m_dbConnection);
            SQLiteDataReader reader = command.ExecuteReader();

            while (reader.Read())
            {
                
                ListViewItem lvi = new ListViewItem(reader["id"].ToString());
                 
                lvi.SubItems.Add(string.Format("{0:d/M/yyyy}", reader["date_intervention"]));
                lvi.SubItems.Add(reader["nom_service"].ToString());
                lvi.SubItems.Add(reader["nom_machine"].ToString());
                listView1.Items.Add(lvi);

            }

            m_dbConnection.Close();

        }

        private void listView1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
    }
}
